package com.global.test.chain.security.authentication;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.global.domain.User;
import com.global.emc.core.CoreUser;
import com.global.emc.core.exception.impl.AuthenticationValidationEMCException;
import com.global.emc.security.authentication.CoreUserAuthenticationHandler;
import com.global.emc.security.confidentiality.encrypt.TextEncryptor;
import com.global.test.service.user.UserService;

/**   
 *  
 * @Description:  [用一句话描述该文件做什么]   
 * @Author:       [Joly_Huang]   
 * @CreateDate:   [2014年3月20日 下午12:24:44]   
 * @UpdateUser:   [Joly_Huang]   
 * @UpdateDate:   [2014年3月20日 下午12:24:44]   
 * @UpdateRemark: [说明本次修改内容]  
 * @Version:      [v1.0] 
 *    
 */
@Component
public class PasswordCoreAuthenticationHandlerImpl implements CoreUserAuthenticationHandler {
	
	private UserService userService;
	private TextEncryptor textEncryptor;
	
	public UserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public TextEncryptor getTextEncryptor() {
		return textEncryptor;
	}
	@Resource(name="bcryptPasswordEncoder")
	public void setTextEncryptor(TextEncryptor textEncryptor) {
		this.textEncryptor = textEncryptor;
	}



	@Override
	public void authentication(CoreUser coreUser) throws AuthenticationValidationEMCException {
		User user = User.convertCoreUser(coreUser);
		String password=user.getPassword();
		
		User u = userService.getUserByUsername(user.getUsername());
		
		if(null == u || !(textEncryptor.matches(u.getPassword(), password)))
			throw new AuthenticationValidationEMCException("Incorrect username or password, Please Try again");
	}

}
